package org.itboys.admin.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.itboys.admin.dto.AdminUserDTO;
import org.itboys.admin.dto.AdminUserData;
import org.itboys.admin.entity.*;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.admin.tools.LoginHolder;
import org.itboys.admin.tools.WebConstants;
import org.itboys.commons.utils.collection.CommonCollectionUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.utils.page.Page;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BusinessCenterService extends BaseAdminService<BusinessCenter, Long> {

	private static final long serialVersionUID = -6380030379631999515L;

	@Resource(name="adminDS")
	private MongoDataSource ds;
	
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<BusinessCenter> getEntityClass() {
		return BusinessCenter.class;
	}

	/**
	 * 根据用户手机号查询用户
	 * 
	 * @param moblie
	 * @return
	 */
	public BusinessCenter getAdminUserByMoblie(String mobile) {
		List<BusinessCenter> list = ds.createQuery(getEntityClass()).filter("isDeleted", 0).filter("mobile", mobile).asList();
		return list==null?null:list.get(0);
	}

	public boolean isExists(String userName) {
		List<BusinessCenter> list = ds.createQuery(getEntityClass()).filter("isDeleted", 0).filter("username", userName).asList();
		return list.size() > 0;
	}

	/**
	 * 根据ID获取管理用户信息
	 * 
	 * @param id
	 * @return
	 */
	public BusinessCenter getUserById(Long id) {
		return getById(id);
	}

	/**
	 * 分页查询
	 * 
	 * @param id
	 * @return
	 */
	public Page<BusinessCenter> pageQueryAll(HttpServletRequest request) {
		return pageQuery(request);}
	
	/**
	 * 修改用户个人信息，不涉及到部门职务关联
	 * 
	 */
	public void doUpdateUserInfo(BusinessCenter user) {
		AdminSessionHolder.prepareAdminLoginData(user);
		update(user);
	}

	public int deleteBy(String userName,Long id) {
		List<BusinessCenter> list = ds.createQuery(getEntityClass()).filter("username", userName).filter("id", id).asList();
        BusinessCenter adminUser = new BusinessCenter();
		if(!list.isEmpty()){
			adminUser = list.get(0);
		}
		int result = update(adminUser.getId(), "isDeleted", 1);
		return result;
	}

	/**
	 * 获取所有的用户列表(带分页)
	 * 
	 * @return
	 */
	public Page<BusinessCenter> findAllUserAndPage(HttpServletRequest request){
		return pageQuery(request);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
